
package com.ssyt.hiring.talent.serlvet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.common.PageModel;
import com.ssyt.hiring.company.entity.ApplicationEntity;
import com.ssyt.hiring.talent.dao.ResumeDao;
import com.ssyt.hiring.talent.entity.ResumeEntity;
import com.ssyt.hiring.talent.entity.TalentEntity;

public class ResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final ResumeDao resumeDao = new ResumeDao();

	public ResumeServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("list".equals(command)) {
			list(request, response);
		} else if ("toAdd".contains(command)) {
			toAdd(request, response);
		} else if ("add".contains(command)) {
			add(request, response);
		} else if ("delete".contains(command)) {
			delete(request, response);
		} else if ("toUpdate".contains(command)) {
			toUpdate(request, response);
		} else if ("update".contains(command)) {
			update(request, response);
		} else if ("toList".contains(command)) {
			tolist(request, response);
		} else if ("deleteApp".contains(command)) {
			deleteApp(request, response);
				
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	private void tolist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/talent/talent-main.jsp")
		.forward(request, response);
	}

	private void toUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		ResumeEntity resumeEntity = resumeDao.list(id);
		request.getSession().setAttribute("resumeEntity", resumeEntity);
		request.getRequestDispatcher("/WEB-INF/views/talent/resume-update.jsp")
				.forward(request, response);

	}

	private void toAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/talent/resume-add.jsp")
				.forward(request, response);
	}

	/**
	 * 修改简历
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ResumeEntity resumeEntity = (ResumeEntity) request.getSession()
				.getAttribute("resumeEntity");
		if (resumeEntity != null) {
			int talentId = resumeEntity.getId();
			String intention = request.getParameter("intention");
			String workExperience = request.getParameter("workExperience");
			String projectExperience = request
					.getParameter("projectExperience");
			// System.out.println(talentId+","+intention+","+workExperience+","+projectExperience);
			ResumeEntity resume = new ResumeEntity();
			resume.setId(talentId);
			resume.setIntention(intention);
			resume.setWorkExperience(workExperience);
			resume.setProjectExperience(projectExperience);
			resumeDao.updateResume(resume);
			// list(request, response);
			response.sendRedirect("ResumeServlet?command=list&pageNo=1&pageSize=10");
		}
	}

	/**
	 * 简历的删除
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int id = Integer.parseInt(request.getParameter("resumeId"));
		//System.out.println(id);
		int row=resumeDao.daleteResume(id);
		if (row == 0) {
			response.getWriter().print("fail");
		}
		// list(request,response);
		//response.sendRedirect("ResumeServlet?command=list&pageNo=1&pageSize=10");
	}

	/**
	 * 简历的添加
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		TalentEntity talentEntity = (TalentEntity) request.getSession()
				.getAttribute("talent");
		if (talentEntity != null) {
			int talentId = talentEntity.getId();
			String intention = request.getParameter("intention");
			String workExperience = request.getParameter("workExperience");
			String projectExperience = request
					.getParameter("projectExperience");
			ResumeEntity resume = new ResumeEntity();
			resume.setTalentId(talentId);
			resume.setIntention(intention);
			resume.setWorkExperience(workExperience);
			resume.setProjectExperience(projectExperience);
			resumeDao.addResume(resume);
			// list(request,response);
			response.sendRedirect("ResumeServlet?command=list&pageNo=1&pageSize=10");

		}

	}

	/**
	 * 首页简历的显示
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		TalentEntity talentEntity = (TalentEntity) request.getSession()
				.getAttribute("talent");
		if (talentEntity != null) {
			int id = talentEntity.getId();
			int pageNo=Integer.parseInt(request.getParameter("pageNo"));
			int pageSize=Integer.parseInt(request.getParameter("pageSize"));
			PageModel<ResumeEntity> pageModel = resumeDao.listResume(id,pageNo,pageSize);
			request.getSession().setAttribute("pageModel", pageModel);
			listapplication(request, response);

		}

	}

	/**
	 * 应聘信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listapplication(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		TalentEntity talentEntity = (TalentEntity) request.getSession()
				.getAttribute("talent");
		if (talentEntity != null) {
			int id = talentEntity.getId();
			List<ApplicationEntity> listapplication = resumeDao.application(id);
			request.getSession().setAttribute("listapplication",
					listapplication);
			request.getRequestDispatcher(
					"/WEB-INF/views/talent/talent-main.jsp").forward(request,
					response);
		}

	}
	
	private void deleteApp(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int id = Integer.parseInt(request.getParameter("listAppId"));
		//System.out.println(id);
		int row=resumeDao.daleteApplication(id);
		if (row == 0) {
			response.getWriter().print("fail");
		}
	}
}
